const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();
let cutTime = null;

Page({
  data: {
    isShow: false,
    isGetCoupon: false,//是否领取到优惠券
    abnorData: {
      id: 1,
      marginTop: 216,
      imgSrc: app.imgUrl + 'abnor-01.png',
      text: '',
      buttonText: '',
      color: '',
      btnTop: 216
    },
    mobiles: '',//授权手机号
    images:{},
    time: 5,
    msg: ''
  },
  onLoad(op) {
    let that = this;
    let options = op || {};
    if (options && options.shopid) {
      app.ws_micro_shop_globalData.shopid = options.shopid;
    }
    let scene = decodeURIComponent(options.scene);
    if (scene && scene !== 'undefined') {
      if (scene.indexOf('=') > 0) {
        let scene_obj = scene.split('&');
        let urlObj = {};
        for (let i = 0; i < scene_obj.length; i++) {
          urlObj[scene_obj[i].split('=')[0]] = scene_obj[i].split('=')[1];
        }
        if (urlObj.shopid) {
          app.ws_micro_shop_globalData.shopid = urlObj.shopid;
        }
		    urlObj.id && (options.id = urlObj.id);
      }
    }
    wx.showLoading({
      title: '加载中...',
    });
    app.ws_micro_shoplogin().then(res => {
      let user_info = wx.getStorageSync('user_info');
      that.setData({
        mobiles: user_info.mobile,
        isMulti: app.ws_micro_shop_globalData.isMulti,
      }, () => {
        that.receiveCoupon(options.id);
      })
    });
    
  },
  onHide() {
    cutTime && clearInterval(cutTime);
  },
  onUnload() {
    cutTime && clearInterval(cutTime);
  },
  imageLoad: function(e) {
    let $width = e.detail.width;    //获取图片真实宽度
    let $height = e.detail.height;
    let images ={
      width: $width,
      height: $height
    }
    this.setData({
      images
    })
  },
  receiveCoupon(id) {
    let that = this;
    // wx.showLoading({
    //   title: '加载中...',
    // });
    app.ws_micro_shoprequest({
      url: api.coupon.receive,
      data: {
        id,
      },
      success: res => {
        if (res.code === 0) {
          let resp  =res.data;
          that.setData({
            goodsId_length: resp.list[0].goods_ids && resp.list[0].goods_ids.length,
            goodsIds: resp.list[0].goods_ids && resp.list[0].goods_ids,
            good_type: resp.list[0].good_type && resp.list[0].good_type || 2,
            good_status: resp.list[0].good_status && resp.list[0].good_status || 2,
            shop_id: resp.list[0].shop_id && resp.list[0].shop_id || 0,
          })
        } else {
          that.setData({
            isGetCoupon: true,
            msg: res.msg
          })
        }
      },
      complete: res => {
        wx.hideLoading();
        that.setData({
          isShow: true,
        })
      }
    });
  },
  bindphone(e) {
    let that = this;
    app.ws_micro_shoplogin(1).then(res => {
      if (e.detail.errMsg === 'getPhoneNumber:ok') {
        app.ws_micro_shoprequest({
          url: api.passport.bind_mobile,
          data: {
            encrypted_data: e.detail.encryptedData,
            iv: e.detail.iv,
          },
          method: 'POST',
          success: function(res) {
            if (res.code === 0) {
              wx.showToast({
                title: '手机号绑定成功',
                icon: 'success',
              });
              that.setData({
                mobile: res.data.mobile,
              });
              that.getCoupon();

              // that.setData({
              //   abnorData: {
              //     id: 4,
              //     marginTop: 256,
              //     imgSrc: app.imgUrl + 'abnor-04.png',
              //     text: '',
              //     buttonText: '重新授权',
              //     color: '#40a6fb',
              //     btnTop: 216
              //   },
              // })
            } else {
              that.setData({
                abnorData: {
                  id: 4,
                  marginTop: 226,
                  imgSrc: app.imgUrl + 'abnor-04.png',
                  text: '',
                  buttonText: '重新授权',
                  color: '#40a6fb',
                  btnTop: 216
                },
              })
            }
          },
        });
      } else {
        wx.showToast({
          title: '您已取消绑定该操作',
          icon: 'none',
        });
      }
    });
  },
  getCoupon() {
    let that = this;
    let { isGetCoupon, msg, time, goodsId_length, good_status } = that.data;
    cutTime && clearInterval(cutTime);
    
    if (isGetCoupon) {
      that.setData({
        abnorData: {
          id: 3,
          marginTop: 168,
          imgSrc: app.imgUrl + 'abnor-03.png',
          text: '',
          buttonText: '去看看其他优惠',
          color: '#40a6fb',
          btnTop: 216
        },
      })
      wx.showToast({
        title: msg,
        icon: 'none',
        duration: 3000,
      });
    } else {
      that.setData({
        abnorData: {
          id: 2,
          marginTop: 216,
          imgSrc: app.imgUrl + 'abnor-02.png',
          text: '领取成功',
          buttonText: '去看看其他优惠',
          color: '#e13212',
          btnTop: 168
        },
      })
      function calc() {
        if (time > 0) {
          time--;
          that.setData({
            time,
          });
        } else {
          cutTime && clearInterval(cutTime);
          that.jumpto();
        }
      }
      if (goodsId_length == 1 && good_status == 1) {
        cutTime = setInterval(() => {
          calc();
        }, 1000);
      }
    }
  },
  jumpto() {
    let { goodsIds, good_type } = this.data;
    let shopid = app.ws_micro_shop_globalData.shopid;
    let url = `/ws_micro_shop/pages/goods/goods?id=${goodsIds[0]}&shopid=${shopid}`;
    if (Number(good_type) === 1) {
      url = `/ws_micro_shop_ms/pages/miaosha/details/details?id=${goodsIds[0]}&shopid=${shopid}`;
    }
    app.util.navigateTo(url, {}, true);
  },
  onClick() {
    let { shop_id, isMulti } = this.data;
    shop_id && (app.ws_micro_shop_globalData.shopid = shop_id);
    let url = '/ws_micro_shop/pages/index/index';
    if (shop_id == 0 && isMulti) {
      url = '/ws_micro_shop/pages/home/index';
    }
    app.util.navigateTo(url, {}, true);
  }
});
